/*
 * Copyright 2008 by Emeric Vernat
 *
 *     This file is part of MSwing.
 *
 * MSwing is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MSwing is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with MSwing.  If not, see <http://www.gnu.org/licenses/>.
 */
package mswing.table;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

/**
 * Allows table columns to be resized to its preferred width by double-clicking at the
 * column-splitter in table header.
 *
 * the preferred width here is the min width reqd to make the data of all rows of that column
 * visible
 *
 * @author Santhosh Kumar T (MySwing)
 */
class MTableColumnAutoResizer extends MouseAdapter {
	/** {@inheritDoc} */
	public void mouseClicked(MouseEvent event) {
		if (event.getClickCount() != 2) {
			return;
		}

		final JTableHeader header = (JTableHeader) event.getSource();
		final TableColumn tableColumn = getResizingColumn(header, event.getPoint());
		if (tableColumn == null) {
			return;
		}
		final int col = header.getColumnModel().getColumnIndex(tableColumn.getIdentifier());
		final JTable table = header.getTable();
		final int rowCount = table.getRowCount();
		int width = (int) header.getDefaultRenderer().getTableCellRendererComponent(table,
				tableColumn.getHeaderValue(), false, false, -1, col).getPreferredSize().getWidth() + 10;
		int preferedWidth;
		for (int row = 0; row < rowCount; row++) {
			preferedWidth = (int) table.getCellRenderer(row, col).getTableCellRendererComponent(
					table, table.getValueAt(row, col), false, false, row, col).getPreferredSize()
					.getWidth();
			width = Math.max(width, preferedWidth);
		}
		header.setResizingColumn(tableColumn); // this line is very important
		tableColumn.setWidth(width + table.getIntercellSpacing().width);
	}

	// copied of BasicTableHeader.MouseInputHandler.getResizingColumn
	private TableColumn getResizingColumn(JTableHeader header, Point p) {
		return getResizingColumn(header, p, header.columnAtPoint(p));
	}

	// copied of BasicTableHeader.MouseInputHandler.getResizingColumn
	private TableColumn getResizingColumn(JTableHeader header, Point p, int column) {
		if (column == -1) {
			return null;
		}
		final Rectangle r = header.getHeaderRect(column);
		r.grow(-3, 0);
		if (r.contains(p)) {
			return null;
		}
		final int midPoint = r.x + r.width / 2;
		int columnIndex;
		if (header.getComponentOrientation().isLeftToRight()) {
			columnIndex = p.x < midPoint ? column - 1 : column;
		} else {
			columnIndex = p.x < midPoint ? column : column - 1;
		}
		if (columnIndex == -1) {
			return null;
		}
		return header.getColumnModel().getColumn(columnIndex);
	}
}
